package com.znjb.myapplication;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * Created by lx on 2016/6/18.
 */
public abstract class LoadingPage extends FrameLayout {
    //定义三个状态
    //正在加载
    public static final int STATE_LOADING = 0;
    //连接错误
    public static final int STATE_ERROR = 1;
    //连接成功
    public static final int STATE_SUCCESS = 2;
    //默认状态为正在加载
    private int mState = STATE_LOADING;
    //正在加载的View
    private View loadingView;
    //加载出错的view
    private View errorView;
    //成功的View
    private View successView;


    public LoadingPage(Context context) {
        super(context);
        initLoadingPage();
    }

    public LoadingPage(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLoadingPage();
    }

    public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLoadingPage();
    }

    /**
     * 向Fragment中添加3个布局
     */
    private void initLoadingPage() {
        ViewGroup.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (loadingView == null) {
            //正在加载的View
            loadingView = View.inflate(getContext(), R.layout.pager_loading, null);
        }
        addView(loadingView, params);

        //加载出错的view
        if (errorView == null) {
            errorView = View.inflate(getContext(), R.layout.pager_error, null);
        }
        addView(errorView, params);

        //加载成功的View
        if (successView == null) {
            successView = createSuccessView();
        }

        if (successView == null) {
            throw new IllegalArgumentException("不能返回空");
        } else {
            addView(successView, params);
        }

        //显示当前的View
        showPage();

        //加载数据并刷新View
        loadViewAndRefresh();

    }

    /**
     * 从服务器加载数据并更新View
     */
    public void loadViewAndRefresh() {
        //访问网络在子线程中进行
        new Thread(){
            @Override
            public void run() {
                //延迟，在真是开发中不这样
                SystemClock.sleep(1500);
                //请求数据
                Object data = loadData();
                //根据访问到的数据是否为空，更新当前的状态
                mState = (data == null ? STATE_ERROR : STATE_SUCCESS);
                //根据当前的状态，更新View,在主线程中进行
                CommonUtil.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        showPage();
                    }
                });
            }
        }.start();
    }



    /**
     * 显示当前正在的View
     */
    public void showPage() {
        //隐藏所有的View
        loadingView.setVisibility(View.INVISIBLE);
        errorView.setVisibility(View.INVISIBLE);
        successView.setVisibility(View.INVISIBLE);
        switch (mState) {
            //正在加载的状态
            case STATE_LOADING:
                loadingView.setVisibility(View.VISIBLE);
                break;
            //加载失败的状态
            case STATE_ERROR:
                errorView.setVisibility(View.VISIBLE);
                break;

            //加载成功的状态
            case STATE_SUCCESS:
                successView.setVisibility(View.VISIBLE);
                break;
        }
    }

    /**
     * 创建成功的View
     *
     * @return
     */
    protected abstract View createSuccessView();

    /**
     * 从服务器请求数据，具体数据不知道，由子类去实现
     * @return
     */
    protected abstract Object loadData();
}
